﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BEPUphysics;
using Microsoft.Xna.Framework;
using PloobsEngine.Physic.PhysicObjects.BepuObject;
using Microsoft.Xna.Framework.Graphics;

namespace PloobsEngine.Modelo
{
    public class OrientedPlaneModel : IModelo
    {
        public OrientedPlaneModel(ref Matrix[] worlds, String texture)
        {
            this.basetexture = texture;
            CreateBillboardVerticesFromList(ref worlds);
            LoadModelo();
        }
        
        string basetexture;
        Texture2D difuse;

        #region IModelo Members        
        BatchInformation[][] batchInformationS = new BatchInformation[1][];
        VertexBuffer[] vertexBufferS = new VertexBuffer[1];
        IndexBuffer[] indexBufferS = new IndexBuffer[1];

        private void CreateBillboardVerticesFromList(ref Matrix[] worlds )
        {
            VertexPositionNormalTexture[] billboardVertices = new VertexPositionNormalTexture[worlds.Count() * 6];
            int i = 0;            

            foreach (var world in worlds)
            {
                billboardVertices[i++] = new VertexPositionNormalTexture(world.Translation,Vector3.TransformNormal(Vector3.Right,world) , new Vector2(0, 0));
                billboardVertices[i++] = new VertexPositionNormalTexture(world.Translation,Vector3.TransformNormal(Vector3.Right,world),  new Vector2(1, 0));
                billboardVertices[i++] = new VertexPositionNormalTexture(world.Translation, Vector3.TransformNormal(Vector3.Right, world), new Vector2(1, 1));

                billboardVertices[i++] = new VertexPositionNormalTexture(world.Translation, Vector3.TransformNormal(Vector3.Right, world), new Vector2(0, 0));
                billboardVertices[i++] = new VertexPositionNormalTexture(world.Translation, Vector3.TransformNormal(Vector3.Right, world), new Vector2(1, 1));
                billboardVertices[i++] = new VertexPositionNormalTexture(world.Translation, Vector3.TransformNormal(Vector3.Right, world), new Vector2(0, 1));
            }

            vertexBufferS = new VertexBuffer[1];
            vertexBufferS[0] = new VertexBuffer(EngineStuff.GraphicsDevice, billboardVertices.Length * VertexPositionNormalTexture.SizeInBytes, BufferUsage.WriteOnly);
            vertexBufferS[0].SetData(billboardVertices);
            indexBufferS = null;
            int noVertices = vertexBufferS[0].SizeInBytes / VertexPositionNormalTexture.SizeInBytes;
            int noTriangles = noVertices / 3;
            BatchInformation[] bi = new BatchInformation[] { new BatchInformation(0, 0, noTriangles, 0, 0, new VertexDeclaration(EngineStuff.GraphicsDevice, VertexPositionNormalTexture.VertexElements), VertexPositionNormalTexture.SizeInBytes, BatchType.NORMAL) };
            batchInformationS[0] = bi;
        }


        public void LoadModelo()
        {            
            
            if (!String.IsNullOrEmpty(basetexture))
            {
                difuse = EngineStuff.CustomContentManager.GetAsset<Texture2D>(basetexture);
            }
 
        }

        public void Serialize(System.Xml.XmlTextWriter textWriter)
        {         
        }

        public void DeSerialize(System.Xml.XmlNode node, EngineStuff engine)
        {            
        }

        public object getModelTag()
        {
            return null;
        }

        public Microsoft.Xna.Framework.Graphics.ModelMesh[] getModelMeshes()
        {
            return null;   
        }

        Matrix[] m = new Matrix[] {Matrix.Identity};
        public Microsoft.Xna.Framework.Matrix[] getBonesTransformation()
        {
            return m;
        }
        
        public Microsoft.Xna.Framework.Graphics.Texture2D getTexture(TextureType textureType)
        {
            switch (textureType)
            {
                case TextureType.DIFFUSE:
                    return difuse;                    
                case TextureType.SPECULAR:
                    break;
                case TextureType.BUMP:
                    break;
                case TextureType.GLOW:
                    break;
                case TextureType.PARALAX:
                    break;
                default:
                    break;
            }
            throw new Exception("Wrong type");
            
        }

        public Microsoft.Xna.Framework.BoundingSphere GetBoudingSphere()
        {
            throw new NotImplementedException();
        }

        public int MeshNumber
        {
            get { return 1; }
        }

        public Microsoft.Xna.Framework.Graphics.VertexBuffer GetVertexBuffer(int meshNumber)
        {
            return vertexBufferS[0];
        }

        public Microsoft.Xna.Framework.Graphics.IndexBuffer GetIndexBuffer(int meshNumber)
        {
            return indexBufferS[0];
        }

        public BatchInformation[] GetBatchInformation(int meshNumber)
        {
            return batchInformationS[0];
        }

        String nome = null;
        public string Name
        {
            get
            {
                return nome;
            }
            set
            {
                this.nome = value;
            }
        }

        #endregion

        #region IModelo Members


        public int GetParentBoneIndex(int meshNumber)
        {
            return 0;
        }

        #endregion
    }
}
